package boun.center;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CalendarAdapter extends BaseAdapter {
	private Context context ;
	private ArrayList<CalendarEvent> events;
	
	public CalendarAdapter(Context c) throws IOException{
		context = c;
		CalendarFetcher fetcher = new CalendarFetcher();
		try {
			events = fetcher.fetchCalendar();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public ArrayList<CalendarEvent> getItems(){
		return events;
	}
	@Override
	public int getCount() {
		return events.size();
	}

	@Override
	public Object getItem(int position) {
		return events.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			LayoutInflater inflater	= LayoutInflater.from(context);
			convertView = inflater.inflate(R.layout.calendar_list_item,parent,false);
		}
		String beginDate = (new SimpleDateFormat("dd/MM/yy")).format(events.get(position).getBeginDate().getTime());
		String endDate = (new SimpleDateFormat("dd/MM/yy")).format(events.get(position).getEndDate().getTime());
		
		TextView EventDate = (TextView) convertView.findViewById(R.id.event_date);
		EventDate.setText(beginDate);
		if(!beginDate.equals(endDate))EventDate.append(" - "+endDate);
		TextView EventName = (TextView) convertView.findViewById(R.id.event_name);
		EventName.setText(events.get(position).getDescriptions().get(0).trim());
		
		
		return convertView;
	}
	
}
